package com.qsmx.store.app.main.mem.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qsmx.store.app.R;
import com.qsmx.store.app.main.mem.entity.MemCardSaleEntity;
import com.qsmx.store.app.main.mem.holder.MemCardSaleSuitHolder;

import java.util.List;

public class MemCardSaleSuitAdapter extends RecyclerView.Adapter<MemCardSaleSuitHolder> {

    private Context context;
    private List<MemCardSaleEntity> mData;

    public MemCardSaleSuitAdapter(Context context) {
        this.context = context;
    }

    //定义接口
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    //声明自定义的监听接口
    private MemCardSaleSuitAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(MemCardSaleSuitAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public void setData(List<MemCardSaleEntity> mData) {
        this.mData = mData;
    }

    @NonNull
    @Override
    public MemCardSaleSuitHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        MemCardSaleSuitHolder holder = new MemCardSaleSuitHolder(LayoutInflater.from(context).inflate(R.layout.adapter_mem_card_suit, viewGroup, false));

        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MemCardSaleSuitHolder memCardSaleSuitHolder, final int position) {

        memCardSaleSuitHolder.tvCardType.setText(mData.get(position).getMemberGrade().getGradeName());
        memCardSaleSuitHolder.tvCardSuitName.setText(String.format(context.getString(R.string.mem_card_sale_suit_name), mData.get(position).getComboName()));
        memCardSaleSuitHolder.tvCardSuitPrice.setText(String.format(context.getString(R.string.mem_card_sale_suit_money), String.valueOf(mData.get(position).getPrice())) + "元");
        memCardSaleSuitHolder.tvCardSuitCoin.setText(String.format(context.getString(R.string.mem_card_sale_suit_coin), String.valueOf(mData.get(position).getCoins())) + "币");

        memCardSaleSuitHolder.cvMemSuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onItemClick(v, position);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() > 0) {
            return mData.size();
        } else {
            return 0;
        }
    }
}
